package com.eastfair.imaster.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.annotation.annotation.base.BaseControllerInfo;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.businpoint.BusinLoadUserRelevancePoint;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.imaster.dto.EmployeeDTO;
import com.eastfair.imaster.dto.EmployeePageQuery;
import com.eastfair.imaster.dto.EmployeeSaveDTO;
import com.eastfair.imaster.dto.EmployeeUpdateDTO;
import com.eastfair.imaster.entity.Employee;
import com.eastfair.imaster.entity.Exhibitor;
import com.eastfair.imaster.service.EmployeeService;
import com.eastfair.imaster.vo.EmployeeVo;
import com.eastfair.core.base.R;
import com.eastfair.echo.core.EchoService;
import com.eastfair.imaster.vo.ImasterVoGroupClass;
import com.eastfair.log.point.PointUtil;
import com.eastfair.vo.VoGroupClass;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.eastfair.core.exception.code.ExceptionCode.VISITOR_MOBILE_ERROR;


/**
 * <p>
 * 前端控制器
 * 公司员工表
 * </p>
 *
 * @author liuxiaobai
 * @date 2021-08-19
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/employee")
@Api(value = "Employee", tags = "公司员工表")
public class EmployeeController extends SuperController<EmployeeService, Long, Employee, EmployeePageQuery, EmployeeSaveDTO, EmployeeUpdateDTO> {

    @Autowired
    private EchoService echoService;

    @Autowired
    private PointUtil pointUtil;
    @Override
    public void handlerResult(IPage<Employee> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }
    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<Employee> employeeList = list.stream().map((map) -> {
            Employee employee = Employee.builder().build();
            //TODO 请在这里完成转换
            return employee;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(employeeList));
    }



    /**
     * 将用户拉入到公司，成为公司员工
     * @param employeeDTO
     * @return
     */
    @ApiOperation(value = "将用户拉入到公司，成为公司员工", notes = "将用户拉入到公司，成为公司员工")
    @PostMapping("/addUserIdOfCompany")
    public R checkCompany(@RequestBody @Validated({EmployeeDTO.addUserIdOfCompany.class }) EmployeeDTO employeeDTO) {
        return baseService.addUserIdOfCompany(employeeDTO);
    }


    /**
     * 公司员工解绑
     * @param employeeDTO
     * companyId      公司id
     * id 员工id
     * @return
     */
    @ApiOperation(value = "公司员工解绑", notes = "公司员工解绑")
    @PostMapping("/breaklUserIdOfCompany")
    public R breaklUserIdOfCompany(@RequestBody @Validated({EmployeeDTO.breaklUserIdOfCompany.class }) EmployeeDTO employeeDTO) {
        return baseService.breaklUserIdOfCompany(employeeDTO.getCompanyId(),employeeDTO.getId());
    }


    /**
     * 查询公司下的员工
     * @param employeeDTO  公司id
     * @return
     */
    @ApiOperation(value = "查询公司下的员工", notes = "查询公司下的员工")
    @PostMapping("/queryEmployee")
    @BaseControllerInfo(voGroup = {ImasterVoGroupClass.queryEmployee.class})
    public R<List<EmployeeVo>> queryEmployee(@RequestBody EmployeeDTO employeeDTO) {
        return R.success(baseService.queryEmployeeOfCompany(employeeDTO));
    }

    /**
     * 员工详情
     * @param id  员工id
     * @return
     */
    @ApiOperation(value = "员工详情", notes = "员工详情")
    @GetMapping("/getById")
    @BaseControllerInfo()
    public R<EmployeeVo> getById(@RequestParam("id")  Long id) {
        EmployeeDTO employeeDTO = new EmployeeDTO();
        employeeDTO.setId(id);
        List<EmployeeVo>  employeeVoList = baseService.queryEmployeeOfCompany(employeeDTO);
        if(employeeVoList==null|| employeeVoList.isEmpty()){
            return R.success(null);
        }
        return R.success(employeeVoList.get(0));
    }

    /**
     * 更换超级管理员
     * @param employeeDTO
     * companyId      公司id
     * id 员工id
     * @return
     */
    @ApiOperation(value = "更换超级管理员", notes = "更换超级管理员")
    @PostMapping("/changeSysAdmin")
    public R changeSysAdmin(@RequestBody @Validated({EmployeeDTO.breaklUserIdOfCompany.class }) EmployeeDTO employeeDTO) {
        return baseService.changeSysAdmin(employeeDTO.getCompanyId(),employeeDTO.getId());
    }




    /**
     * 将用户拉入到公司，成为公司员工
     * @param employeeDTO
     * @return
     */
    @ApiOperation(value = "将用户拉入到公司，成为公司员工", notes = "将用户拉入到公司，成为公司员工")
    @PostMapping("/addUserOfCompany")
    public R addUserOfCompany(@RequestBody @Validated({EmployeeDTO.addUserIdOfCompany.class }) EmployeeDTO employeeDTO) {
        return baseService.addUserIdOfCompany(employeeDTO);
    }


    /**
     * 修改员工用户基本信息
     * @param employeeDTO
     * @return
     */
    @ApiOperation(value = "修改员工用户基本信息", notes = "修改员工用户基本信息")
    @PutMapping("/updateCompanyUser")
    public R updateCompanyUser(@RequestBody EmployeeDTO employeeDTO) {
        return baseService.updateCompanyUser(employeeDTO);
    }


    /**
     * 测试
     *
     * @return
     */
    @ApiOperation(value = "测试", notes = "测试")
    @GetMapping("/test")
    public R<String> test() {

        List<Employee> employeeList =  baseService.list(null,null);
        if(employeeList==null || employeeList.isEmpty()){
            return R.success(null);
        }
        for(Employee employee : employeeList){
            BusinLoadUserRelevancePoint businLoadUserRelevancePoint = new BusinLoadUserRelevancePoint();
            businLoadUserRelevancePoint.setUserId(employee.getUserAccountId());
            businLoadUserRelevancePoint.setCompanyId(employee.getCompanyId());
            businLoadUserRelevancePoint.setProjectId(employee.getProjectId());
            pointUtil.addBusinPointMsg(businLoadUserRelevancePoint);
        }
//        R<String> r = userAccountServiceFeign.test();
//        log.info(JSON.toJSONString(r));
//        return R.success(null);
        return R.fail(VISITOR_MOBILE_ERROR);
    }

}
